<template>
  <div class='container'>
    <el-dialog
  :title="pageTitle"
  :visible="dialogShow"
  @close="close"
  width="20%"
  >
 <el-form :model="form" ref="form" label-width="80px" :rules="rules">
  <el-form-item label="学科名称"  prop="value" v-show="!subjectID">
     <el-select v-model="form.subjectID" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
  <el-form-item label="目录名称"  prop="directoryName"   >
    <el-input v-model.trim="form.directoryName" autocomplete="off"></el-input>
  </el-form-item>
</el-form>

  <span slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="submit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { add, update, detail, simple } from '@/api/hmmm/directorys.js'
export default {
  props: ['dialogShow', 'pageTitle', 'subjectID'],
  data () {
    return {
      form: {
        directoryName: '',
        subjectID: '',
        id: null
      },
      options: [],
      rules: {
        directoryName: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }],
        subjectID: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }]
      }
    }
  },
  async created () {
    const res = await simple()

    this.options = [...res.data]
  },
  methods: {
    async hanldeEditForm (id) {
      this.form.id = id
      const { data } = await detail(this.form)

      this.form.subjectID = data.subjectID
      this.form.directoryName = data.directoryName
    },

    close () {
      this.$refs.form.resetFields()
      this.form.subjectID = ''
      this.form.directoryName = ''
      this.$emit('update:dialogShow', false)
    },
    async submit () {
      try {
        await this.$refs.form.validate()

        if (this.form.id) {
          await update(this.form)
          this.$message.success('修改成功')
        } else {
          this.form.subjectID = this.subjectID
          await add(this.form)
          this.$message.success('添加成功')
        }
        this.close()
        this.$parent.initData()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.dialog-footer{
  margin-left:135px ;
}
</style>
